﻿namespace Foggy.ViewModels 
{
    using System.ComponentModel.Composition;
    using System.Reflection;

    using Caliburn.Micro;

    using Foggy.Data;
    using Foggy.Settings;
    using Foggy.Users;

    /// <summary>
    /// Shell view model
    /// </summary>
    [Export(typeof(IShellViewModel))]
    public sealed class ShellViewModel : Conductor<IScreen>, IShellViewModel
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ShellViewModel"/> class.
        /// </summary>
        /// <param name="windowManager">The window manager.</param>
        /// <param name="settingsService">The settings service.</param>
        /// <param name="userService">The user service.</param>
        [ImportingConstructor]
        public ShellViewModel(IWindowManager windowManager, ISettingsService settingsService, IUserService userService)
        {
            this.WindowManager = windowManager;
            this.SettingsService = settingsService;
            this.UserService = userService;

            var version = Assembly.GetEntryAssembly().GetName().Version;
            this.DisplayName = string.Format("Foggy v{0}", version);

            var fogBugzUrl = this.SettingsService.GetSettingOrDefault(Setting.FogBugzUrl, string.Empty);
            
            // TODO: error if url is empty
            this.FogBugzRepository = new FogBugzRepository(fogBugzUrl);
        }

        /// <summary>
        /// Gets or sets the user service.
        /// </summary>
        /// <value>
        /// The user service.
        /// </value>
        private IUserService UserService { get; set; }

        /// <summary>
        /// Gets or sets the window manager.
        /// </summary>
        /// <value>
        /// The window manager.
        /// </value>
        private IWindowManager WindowManager { get; set; }

        /// <summary>
        /// Gets or sets the settings service.
        /// </summary>
        /// <value>
        /// The settings service.
        /// </value>
        private ISettingsService SettingsService { get; set; }

        /// <summary>
        /// Gets or sets the fog bugz repository.
        /// </summary>
        /// <value>The fog bugz repository.</value>
        private IFogBugzRepository FogBugzRepository { get; set; }

        /// <summary>
        /// Shows the settings.
        /// </summary>
        public void ShowSettings()
        {
            var settingsViewModel = new SettingsViewModel(new SettingsService(), new UserService());
            var saved = this.WindowManager.ShowDialog(settingsViewModel, null, null);
        }

        /// <summary>
        /// Called when initializing.
        /// </summary>
        protected override void OnInitialize()
        {
            base.OnInitialize();

            var loginUsername = this.SettingsService.GetSettingOrDefault(Setting.LoginUsername, string.Empty);
            var loginPassword = this.SettingsService.GetSettingOrDefault(Setting.LoginPassword, string.Empty);

            // TODO: error is login unsuccessful
            var success = this.FogBugzRepository.LogOn(loginUsername, loginPassword);            
            
            this.ActivateItem(new MainViewModel(this.FogBugzRepository, this.UserService));
        }

        /// <summary>
        /// Called when deactivating.
        /// </summary>
        /// <param name="close">Inidicates whether this instance will be closed.</param>
        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);

            if (close)
            {
                this.FogBugzRepository.LogOff();
            }
        }
    }
}